package com.media.photocamera.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.media.photocamera.utils.CameraUiUtil;


public class CutMarkView extends View {
    public int width;//屏幕宽度
    public int height;//屏幕高度
    public int maskWidth;//中部透明区域宽度
    public int maskHeight;//中部透明区域高度
    public double maskFloat;

    private Paint linePaint;
    private Paint rectPaint;
    private Paint topTextPaint;
    private Paint bottomTextPaint;

    public void setMaskSize(int maskWidth, int maskHeight, int width, int height) {
        this.maskHeight = maskHeight;
        this.maskWidth = maskWidth;
        this.width = width;
        this.height = height;
        Log.d("CutMarkView","maskHeight "+maskHeight+"maskWidth "+maskWidth+" width"+width+" height"+height);
        invalidate();
    }

    public CutMarkView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CutMarkView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    public CutMarkView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        //绘制中间透明区域矩形边界的Paint
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(Color.TRANSPARENT);//设置中间区域颜色为透明
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(3f);
        linePaint.setAlpha(0);//取值范围为0~255，数值越小越透明

        //绘制四周矩形阴影区域
        rectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        rectPaint.setColor(Color.BLACK);
        rectPaint.setStyle(Paint.Style.FILL);
        rectPaint.setAlpha(170);//取值范围为0~255，数值越小越透明

        //绘制顶部中间提示字体
        topTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        topTextPaint.setColor(Color.WHITE);
        //topTextPaint.setStyle(Paint.Style.FILL);
        topTextPaint.setTextAlign(Paint.Align.CENTER);//把x,y坐标放到字体中间（默认x,y坐标是字体头部）
        topTextPaint.setTextSize(CameraUiUtil.sp2px(context, 14));

        //绘制顶部中间提示字体
        bottomTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bottomTextPaint.setColor(Color.parseColor("#A0A0A0"));
        //bottomTextPaint.setStyle(Paint.Style.FILL);
        bottomTextPaint.setTextAlign(Paint.Align.CENTER);//把x,y坐标放到字体中间（默认x,y坐标是字体头部）
        bottomTextPaint.setTextSize(CameraUiUtil.sp2px(context, 12));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (maskHeight == 0 && maskWidth == 0) {
            return;
        }
        if (maskHeight == height || maskWidth == width) {
            return;
        }
        //height：屏幕高度
        //width：屏幕宽度
        //maskHeight：中间透明区域高度
        //maskWidth：中间透明区域宽度
        int h = Math.abs((height - maskHeight) / 2);//顶部阴影高度
        int w = Math.abs((width - maskWidth) / 2);//右侧阴影宽度
        //上阴影
        canvas.drawRect(0, 0, width, h, this.rectPaint);
        //右阴影
        canvas.drawRect(width - w, h, width, height - h, this.rectPaint);
        //下阴影
        canvas.drawRect(0, height - h, width, height, this.rectPaint);
        //左阴影
        canvas.drawRect(0, h, w, h + maskHeight, this.rectPaint);
        //中透明
        canvas.drawRect(w, h, w + maskWidth, h + maskHeight, this.linePaint);
        canvas.save();//保存上面的上下左右中
        //中-顶部-字体
//            canvas.rotate(90, width - w / 2, height / 2);//把画布旋转旋转90度
        canvas.rotate(0, 0, 0);//把画布旋转旋转90度
//            canvas.drawText("请将本人头像对准框内", width - w / 2, height / 2, topTextPaint);
        canvas.drawText("请将本人头像对准框内", width / 2, h / 2, topTextPaint);
        canvas.restore();//把画布恢复到上次保存的位置，防止本次旋转影响下面的操作
        canvas.save();//保存中顶部字体
        //中-底部-字体
//            canvas.rotate(90, w / 2, height / 2);//旋转90度
//            canvas.rotate(0, 0 , 0);//旋转90度
//            canvas.drawText("请保持光线充足，背景干净，手机与卡片持平", width  / 2, height / 2, bottomTextPaint);
//            canvas.restore();//把画布恢复到上次保存的位置，防止本次旋转影响下面的操作
//            canvas.save();//保存中底部字体
//            //画图片，就是贴图
////            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_face_border);
////            canvas.rotate(90, width - w  - 80, height - h - bitmap.getHeight() - 30);//把画布旋转旋转90度
////            canvas.drawBitmap(bitmap, width - w  - 80, height - h - bitmap.getHeight() - 30, new Paint());
        //打印logo
//            Log.e("高度宽度", "height:" + height + ",width:" + width + ",h:" + h + ",w:" + w + ",mskHeight:" + maskHeight + ",maskWidth:" + maskWidth);
        super.onDraw(canvas);
    }
}
